package cn.kxm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.kxm.pojo.Dict;
import cn.kxm.pojo.DictDetail;
import cn.kxm.pojo.Role;
import cn.kxm.pojo.Sysuser;
import cn.kxm.service.AdministratorService;

@Controller
@RequestMapping("/administrator")
public class AdministratorController {
     @Autowired
     @Qualifier("AdministratorServiceImpl")
     private AdministratorService as;
     
     @RequestMapping("findall")
     @ResponseBody
     public Object findAll(@RequestParam(value="userName",required=false)String userName,Integer page,Integer rows){
    	 List<Map<String,Object>> list=as.findAdministratorList(userName,(page-1)*rows,rows); 
    	 int total=as.getAdministratorCount(userName);
    	 Map<String,Object> map=new HashMap<String, Object>();
 		 map.put("total",total);
 		 map.put("rows",list);
 		 return JSONArray.toJSONString(map);
     }
     
     @RequestMapping("findalltype")
 	 @ResponseBody
 	 public Object findAllType(){
        List<Role> list=as.getRoles();
        return JSONArray.toJSONString(list);	 
     }
     
     @RequestMapping("checkUserName")
 	 @ResponseBody
 	 public Object checkUserName(String userName){
     	Sysuser sysuser=as.checkUserName(userName);
     	Map<String,Object> map=new HashMap<String, Object>();
     	if(sysuser!=null){
     		map.put("result","cunzai");
     	}else{
     		map.put("result","bucunzai");
     	}
     	return JSONArray.toJSONString(map);
     }
     
        @RequestMapping("addAdministrator")
    	@ResponseBody
    	public Object addAdministrator(Sysuser sysuser){
     	Map<String, Object> map=new HashMap<String, Object>();
     	if(as.addAdministrator(sysuser)==1){
     		map.put("result","success");
     	}else{
     		map.put("result","error");
     	}
     	return JSONArray.toJSONString(map);
        }
        
        @RequestMapping("updateAdministratorById")
    	public String updateAdministratorById(Model model,Integer id){
        	Map<String,Object> sysuser=as.getAdministratorById(id);
        	model.addAttribute("user",sysuser);
        	return "web/goods/updateAdministrator";
        }
        
        @RequestMapping("showAdministratorById")
    	public String showAdministratorById(Model model,Integer id){
        	Map<String,Object> sysuser=as.getAdministratorById(id);
        	model.addAttribute("user",sysuser);
        	return "web/goods/showAdministrator";
        }
        
        @RequestMapping("updateAdministrator")
    	@ResponseBody
    	public Object updateAdministrator(Sysuser sysuser){
     	Map<String, Object> map=new HashMap<String, Object>();
     	if(as.updateAdministrator(sysuser)==1){
     		map.put("result","success");
     	}else{
     		map.put("result","error");
     	}
     	return JSONArray.toJSONString(map);
        }
        
        @RequestMapping("/delAdministrator")
        @ResponseBody
        public Object delAdministrator(Integer id){
    		Map<String,Object> map=new HashMap<String, Object>();
    		if(as.delAdministrator(id)==1){
    			map.put("result","success");
    		}else{
    			map.put("result","error");
    		}
        	return JSON.toJSONString(map);
        }
}
